<?php
/**
 * Created by PhpStorm.
 * User: 84071
 * Date: 2017-09-07
 * Time: 11:47
 */
namespace app\common\model;
use think\Model;

class OrderShop extends Model{
    /**
     * @param $uid 接收用户uid
     * @return array 如果不存在就返回空，否则处理商品列表并返回
     */
    public function get_list($data){
        $get_list = $this->where('uid',$data['uid'])->where('mid',$data['mid'])->find();
        if(empty($get_list) || empty($get_list['aids'])){
            return $get_list;
        }
        $get_list = $get_list->toArray();
        $aids = unserialize($get_list['aids']);
        $shopmodel = new Article();
        $artmodel = new ArtModel();
        foreach ($aids as $k => $v){
            $shop = $shopmodel->where('aid',$v['aid'])->find();
            $shop = $shopmodel->get_onedata($shop);
            //  检测是否存在促销字段
            $files = $artmodel->editfile($shop['mid']);
            $shopsales = null;
            foreach ($files as $k1 => $v1){
                if($v1['formtype'] == 'shopsales'){
                    $shopsales = $k1;
                    break;
                }
            }
            if(!empty($shopsales) && $shop[$shopsales]){
                foreach ($shop[$shopsales] as $k2 => $v2){
                    $volume[$k2]  = $v2['title'];
                    $edition[$k2] = $v2['value'];
                }
                $ret = arraySort($shop[$shopsales], 'title');
                $v['shopsales'] = array_values($ret);
                $dnum = '0';
                foreach ($v['shopsales'] as $k2 => $v2){
                    if($v['num'] > $dnum && $v['num'] <= $v2['title']){
                        $v['newmoney'] = $v['money']['money'] * $v2['value'] / 10;
                        break;
                    }
                }
            }
            $v['shop'] = $shop;
            $aids[$k] = $v;
        }
        $get_list['aids'] = $aids;
        return $get_list;
    }

    /**
     * @param $data 接收要添加的数组
     * @return bool 返回处理结果是否成功
     */
     public function addOne($data){
         // 查询产品信息
         $shopmodel = new Article();
         $shop = $shopmodel->where('aid',$data['aid'])->find();
         $shop = $shopmodel->get_onedata($shop);
         // 查找产品价格并写入,如果是默认的，就调用默认价格，否则写入选项价格
         $moneys = $data['money'];
         if($moneys['title'] == '0'){
             $moneys['money'] = $shop['money'];
         }else{
             $moneyclass = $moneys['title'];
             foreach ($shop[$moneyclass] as $v){
                 if($v['title'] == $data['money']['value']){
                     $moneys['money'] = $v['value'];
                     continue;
                 }
             }
         }
         $shopfile = empty($data['shopfile']) ? null:$data['shopfile'];
         $data['money'] = $moneys;
         $aids = array(
             'aid' => $data['aid'],
             'title' => $shop['title'],
             'picurl' => $shop['picurl'],
             'num' => $data['num'],
             'money' => $data['money'],
             'shopfile' => $shopfile
         );
         // 查询购物车是否有商品存在
         $olddata = $this->where('uid',$data['uid'])->where('mid',$shop['mid'])->find();
         if(empty($olddata)){
             $data['mid'] = $shop['mid'];
             $data['aids'][] = $aids;
             $data['aids'] = serialize($data['aids']);
             if($this->allowField(true)->save($data)){
                 return true;
             }
         }else{
             // 进行数据对比
             $oldaids = unserialize($olddata['aids']);
             $newadd = true;
             if(!empty($oldaids)){
                 foreach ($oldaids as $k => $v){
                     if($v['aid'] == $aids['aid']){
                         $dadis = $aids;
                         unset($dadis['num']);
                         $dnew = $v;
                         unset($dnew['num']);
                         if(serialize($dadis) == serialize($dnew)){
                             $v['num'] = intval($v['num']) + intval($aids['num']);
                             $newadd = false;
                         }
                     }
                     $newaids[$k] = $v;
                 }
             }
             if($newadd){
                 $newaids[] = $aids;
             }
             $olddata['aids'] = serialize($newaids);
             if($this->allowField(true)->isUpdate(true)->save($olddata->toArray(),$olddata['id'])){
                 return true;
             }
         }
         return false;
     }

    /**
     * @param $data 获取要处理的数组 del为要删除的产品键值 和 aids 对应
     * @return bool 返回处理是否成功
     */
    public function delList($data){
        foreach ($data['del'] as $k => $v){
            unset($data['aids'][$v]);
        }
        $olddata = $this->where('id',$data['id'])->where('uid',$data['uid'])->find();
        if(empty($olddata)){
            return false;
        }
        $olddata = $olddata->toArray();
        $olddata['aids'] = serialize($data['aids']);
        if($this->allowField(true)->isUpdate(true)->save($olddata,['id'=>$olddata['id']])){
            return true;
        }
        return false;
    }

    /**
     * @param $id 接收要清空的id
     */
    public function alldel($id){
        $olddata = $this->where('id',$id)->find()->toArray();
        $olddata['aids'] = null;
        if($this->allowField(true)->isUpdate(true)->save($olddata,['id'=>$olddata['id']])){
            return true;
        }
    }
}